package controllers;

import models.TinTuc;
import models.crudsiena.SienaSupport;
import play.exceptions.TemplateNotFoundException;
import play.i18n.Lang;
import play.i18n.Messages;
import play.mvc.With;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * Created by Gipxy group
 * Date: 4/5/12
 */
@With(Secure.class)
public class TinTucs extends CRUD {
    public static void list(int page, String filterField, String filterValue, String orderBy, String order) {
        ObjectType type = ObjectType.get(getControllerClass());
        notFoundIfNull(type);
        if (page < 1) {
            page = 1;
        }

        List<? extends SienaSupport> objects = type.findPage(page, filterField, filterValue, orderBy, order);
        Long count = type.count(filterField, filterValue);
        Long totalCount = type.count(null, null);
        try {
            render(type, objects, count, totalCount, page, orderBy, order);
        } catch (TemplateNotFoundException e) {
            render("CRUD/list.html", type, objects, count, totalCount, page, orderBy, order);
        }
    }

    public static void save(String id) throws Exception {
        ObjectType type = ObjectType.get(getControllerClass());
        notFoundIfNull(type);
        SienaSupport object = type.findById(id);

        object = object.edit("object", params);
        // Look if we need to deserialize
        for (ObjectField field : type.getFields()) {
            if (field.type.equals("serializedText") && params.get("object." + field.name) != null) {
                Field f = object.getClass().getDeclaredField(field.name);
                f.set(object, CRUD.collectionDeserializer(params.get("object." + field.name),(Class)((ParameterizedType) f.getGenericType()).getActualTypeArguments()[0]));
            }
        }

        validation.valid(object);
        if (validation.hasErrors()) {
            renderArgs.put("error", Messages.get("crud.hasErrors"));
            try {
                render(request.controller.replace(".", "/") + "/show.html", type, object);
            } catch (TemplateNotFoundException e) {
                render("CRUD/show.html", type, object);
            }
        }

        //hoailt add 2 lines
        ((TinTuc)object).setTimeModified(params.get("object.timeModified"));
        ((TinTuc)object).setLanguage(Lang.get());

        object.update();
        flash.success(Messages.get("crud.saved", type.modelName, object.getEntityId()));
        if (params.get("_save") != null) {
            redirect(request.controller + ".list");
        }
        redirect(request.controller + ".show", object.getEntityId());
    }

    public static void create() throws Exception {
        ObjectType type = ObjectType.get(getControllerClass());
        notFoundIfNull(type);
        SienaSupport object = type.entityClass.newInstance();

        validation.valid(object.edit("object", params));

        if (validation.hasErrors()) {
            renderArgs.put("error", Messages.get("crud.hasErrors"));
            try {
                render(request.controller.replace(".", "/") + "/blank.html", type);
            } catch (TemplateNotFoundException e) {
                render("CRUD/blank.html", type);
            }
        }
        //hoailt add 2 lines
        ((TinTuc)object).setTimeModified(params.get("object.timeModified"));
        ((TinTuc)object).setLanguage(Lang.get());

        object.insert();
        flash.success(Messages.get("crud.created", type.modelName, object.getEntityId()));
        if (params.get("_save") != null) {
            redirect(request.controller + ".list");
        }
        if (params.get("_saveAndAddAnother") != null) {
            redirect(request.controller + ".blank");
        }
        redirect(request.controller + ".show", object.getEntityId());
    }
}
